package com.spt.springbootzhsx.controller;

import com.spt.springbootzhsx.entity.Movie;
import com.spt.springbootzhsx.entity.User;
import com.spt.springbootzhsx.service.MovieService;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;

@RestController
@RequestMapping(value="/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;

    @PostMapping(value="/addMovie")
    public String addMovie(@RequestBody Movie movie){
        return movieService.addMovie(movie);
    }

    @GetMapping(value="/getMovieByMid/{mid}")
    public Movie getMovieByMid(@PathVariable("mid") Integer mid){
        return movieService.getMovieByMid(mid);
    }

    @PostMapping(value = "/updateMovie")
    public String updateMovie(@RequestBody Movie movie){
        return movieService.updateMovie(movie);
    }

    @GetMapping(value="/deleteMovie/{mid}")
    public String deleteMovie(@PathVariable("mid") Integer mid){
        return movieService.deleteMovie(mid);
    }

    @GetMapping(value="/getAllMovie")
    public ArrayList<Movie> getAllMovie() {
         return movieService.getAllMovie();
    }

    @GetMapping(value="/getMovieByMstate/{mstate}")
    public ArrayList<Movie> getAllMovieByMstate(@PathVariable("mstate") String mstate){
        return movieService.getMovieByMstate(mstate);
    }

    @GetMapping(value="/getTop5Movie")
    public ArrayList<Movie> getTop5Movie(){
        return movieService.getTop5Movie();
    }

    @GetMapping(value="/getTop7Movie")
    public ArrayList<Movie> getTop7Movie(){
        return movieService.getTop7Movie();
    }

    @GetMapping(value="/getMovieByMname/{mname}")
    public ArrayList<Movie> getMovieByMname(@PathVariable("mname") String mname){
        return movieService.getMovieByMname(mname);
    }

    @GetMapping(value = "/getMovieByKeyAndMstate/{key}/{mstate}")
    public ArrayList<Movie> getMovieByKeyAndMstate(@PathVariable("key") String key,@PathVariable("mstate") String mstate){
        return movieService.getMovieByKeyAndMstate(key,mstate);
    }

    @GetMapping(value="/getMovieByMtype/{mtype}")
    public ArrayList<Movie> getMovieByMtype(@PathVariable("mtype") String mtype) {
        return movieService.getMovieByMtype(mtype);
    }

    @GetMapping(value="/getMovieByIndex/{start}/{num}")
    public ArrayList<Movie> getMovieByIndex(@PathVariable("start") Integer start,@PathVariable("num") Integer num){
        return movieService.getMovieByIndex(start,num);
    }

    @PostMapping(value = "/uploadImg/{mid}")
    public String uploadImg(@RequestBody MultipartFile file, @PathVariable("mid") Integer mid){
        try{
            System.out.println("**********正在上传*********");

            Movie movie=movieService.getMovieByMid(mid);
            //文件名
            String filename = movie.getMname();
            //文件大小
            Long size = file.getSize();
            //文件类型
            String type = file.getContentType();

            String filePath = "D://Web课设前端//web-vue2//src//assets//" + filename+".jpg";
            System.out.println("文件存储路径为：" + filePath);

            File demoFile = new File(filePath);
            file.transferTo(demoFile);

            String src = "assets/"+filename;
            movie.setPic(filename+".jpg");
            System.out.println("上传成功！修改数据库pic.....");
            String str=movieService.updateMovie(movie);
            if(str.equals("电影更新成功！"))
                 return "上传成功";
            else
                return "上传失败，电影更新失败！";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return  "上传失败";
        }
    }
}
